<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="getCategory.top" :to="`/category/${getCategory.top.id}`">{{ getCategory.top.name }}
    </XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem v-if="getCategory.sub" :key="getCategory.sub.id">{{ getCategory.sub.name }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script lang="ts" setup>
import { useRoute } from 'vue-router'
import { computed } from 'vue'
import categoryStore from '../../store/index'
// 1. 获取二级分类的ID，在地址在路由中
const route = useRoute()
// 2. 获取vuex中的类目数据
const { category } = categoryStore()
// 3. 通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID
const getCategory = computed(() => {
  const obj: any = {}
  // 第一层循环拿到顶级类目数据
  category.list.forEach(top => {
    // 存在顶级类目再执行第二层循环拿到二级类目数据
    top.children && top.children.forEach(sub => {
      if (sub.id === route.params.id) {
        // 设置二级类目
        obj.sub = { id: sub.id, name: sub.name }
        // 设置一级类目
        obj.top = { id: top.id, name: top.name }
      }
    })
  })
  return obj
})
</script>
<style  lang="less" scoped>

</style>